package com.lolo.controller;

import java.util.HashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lolo.dao.UserMapper;
import com.lolo.datasource.annotation.DataSource;

@RestController
@RequestMapping("/users")
public class UserController {

	private static final Logger logger = LoggerFactory.getLogger(UserController.class);

	@Autowired
	private UserMapper userMapper;

	@GetMapping("/msg")
	public Object msg() {
		HashMap<String, Object> map = new HashMap<>();
		map.put("msg", "OK");
		return map;
	}

	/**
	 * 默认数据源
	 */
	@GetMapping("/t1")
	public Object users() {
		logger.info("控制器:[{}],DAO:[{}]", getClass().getName(), userMapper);
		HashMap<String, Object> map = new HashMap<>();
		map.put("msg", "OK");
		map.put("data", userMapper.selectList(null));
		return map;
	}

	/**
	 * 默认数据源
	 */
	@GetMapping("/t2")
	@DataSource()
	public Object users2() {
		logger.info("控制器:[{}],DAO:[{}]", getClass().getName(), userMapper);
		HashMap<String, Object> map = new HashMap<>();
		map.put("msg", "OK");
		map.put("data", userMapper.selectList(null));
		return map;
	}

	/**
	 * slave1数据源
	 */
	@DataSource("slave1")
	@GetMapping("/t3")
	public Object users3() {
		logger.info("控制器:[{}],DAO:[{}]", getClass().getName(), userMapper);
		HashMap<String, Object> map = new HashMap<>();
		map.put("msg", "OK");
		map.put("data", userMapper.selectList(null));
		return map;
	}

	/**
	 * slave2数据源
	 */
	@DataSource("slave2")
	@GetMapping("/t4")
	public Object users4() {
		logger.info("控制器:[{}],DAO:[{}]", getClass().getName(), userMapper);
		HashMap<String, Object> map = new HashMap<>();
		map.put("msg", "OK");
		map.put("data", userMapper.selectList(null));
		return map;
	}

}
